<?php

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

class Goods extends Model
{   
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $schema = [
        'id' => 'int',
        'category_id' => 'int',
        'sort' => 'int',
        'goods_name' => 'string',
        'main_image' => 'string',
        'goods_images' => 'json',
        'goods_attr' => 'json',
        'total_stock' => 'int',
        'total_sale' => 'int',
        'market_low_price' => 'float',
        'sell_low_price' => 'float',
        'integral' => 'int',
        'status' => 'int',
        'create_time' => 'timestamp',
        'update_time' => 'timestamp',
        'delete_time' => 'timestamp', 
    ];

    protected $readonly = ['goods_attr'];

    public function getGoodsImagesAttr($value)
    {
        return json_decode($value,true);
    }

    public function getGoodsAttrAttr($value)
    {   
        $arr = [];
        $attrs =  json_decode($value,true);
        
        foreach($attrs as $v){
            array_push($arr,array_key_first($v));
        }

        return $arr;
    }

    public function goodsContent()
    {
        return $this->hasOne(GoodsContent::class);
    }

    public function items()
    {
        return $this->hasMany(GoodsItem::class);
    }

    public function category()
    {
        return $this->belongsTo(GoodsCategory::class,'category_id','id');
    }
}